﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.Common;
using Dapper;
using System.Linq;

namespace AgencyManagerSystem.DataFetcher
{
    public class LoginFetcher:BaseFetcher<LoginModel>
    {
        public LoginFetcher() : base()
        {
            
        }
        private class PasswordStatus
        {
            public string Password { get; set; }
            public int Status { get; set; }
            public int UserId { get; set; }
        }
        public bool GetForLogin(string code,string pwd,out string message,out int userid)
        {
            string pwd_sha = Util.ToSHA1(pwd);
            if (String.IsNullOrWhiteSpace(code) || String.IsNullOrWhiteSpace(pwd))
            {
                userid = 0;
                message = "登录名或密码为空";
                return false;
            }
            string sqlstr = "SELECT USERID,PASSWORD,STATUS FROM SYS_USER USR LEFT JOIN SYS_LOGIN LOG ON USR.AUTOID = LOG.USERID WHERE CODE = @CODE ";
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("@CODE", code);
            IList<PasswordStatus> result = null;
            try
            {
                result = DBHelper.GetTran().Query<PasswordStatus>(sqlstr, dynamicParameters).AsList();
            }
            catch(Exception ex)
            {
                LogHelper.Err(this.GetType().Name, ex.Message + "-" + ex.StackTrace);
                result = new List<PasswordStatus>();
            }
            if(result.Count==0)
            {
                message = "用户名不正确";
                userid = 0;
                return false;
            }
            PasswordStatus swap = result.First();
            if(swap.Password!= pwd_sha)
            {
                message = "密码错误";
                userid = 0;
                return false;
            }
            if(swap.Status!=1)
            {
                message = "账户被锁定";
                userid = swap.UserId;
                return false;
            }
            message = "操作成功";
            userid = swap.UserId;
            return true;
        }
        public LoginModel GetByUserId(int userId)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "UserId", Op = "=", Value = userId } };
            var list = List(conditions);
            if (list.Count > 0)
                return list.First();
            else
                return null;
        }
        public RoleTableModel GetRightTableByLogin(int uid, int domain, int module,out string message)
        {
            string sqlstr = "SELECT * FROM SYS_ROLETABLE WHERE ROLE IN (SELECT ROLE FROM SYS_ROLEUSER WHERE USER = @UID AND DOMAIN = @DOMAIN AND MODULE = @MODULE) ";
            DynamicParameters dynamicParameters = new DynamicParameters();
            dynamicParameters.Add("@UID", uid);
            dynamicParameters.Add("@DOMAIN", domain);
            dynamicParameters.Add("@MODULE", module);
            IList<RoleTableModel> ls = null;
            RoleTableModel result = null;
            try
            {
                ls = DBHelper.GetTran().Query<RoleTableModel>(sqlstr, dynamicParameters).AsList();
            }
            catch (Exception ex)
            {
                LogHelper.Err(this.GetType().Name, ex.Message + "-" + ex.StackTrace);
                result = new RoleTableModel();
            }
            if (ls.Count == 0)
            {
                message = String.Format("GETRIGHTTABLEBYLOGIN:无法取得Uid={0},Domain={1},Module={2}的权限",uid,domain,module);
                result = new RoleTableModel();
            }
            else
            {
                message = String.Format("GETRIGHTTABLEBYLOGIN:取得Uid={0},Domain={1},Module={2}的权限成功", uid, domain, module);
                result = ls.First();
            }
            return result;
        }
    }
}
